package com.xjscrm.server.common.util;

import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.Tools;
import com.xjscrm.common.vo.newcustmoerplan.NewCustomerPlanRule;
import com.xjscrm.common.vo.newcustmoerplan.SleepRule;
import com.xjscrm.common.vo.operatorplandevice.OperatorPlanDeviceRule;
import com.xjscrm.common.vo.sop.SopDeviceRule;

import java.time.*;
import java.util.Date;
import java.util.List;

/**
 * @author lucher.chen create in 2024/3/1 18:25
 */
public class SleepTimeTools {

    /**
     * 验证是否是机器人休眠时间段（true-休眠；false-不休眠）
     *
     * @param operatorPlanDeviceRule
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static boolean verifySleepTime(OperatorPlanDeviceRule operatorPlanDeviceRule) {
        if (operatorPlanDeviceRule != null) {
            if (Tools.getInteger(operatorPlanDeviceRule.getSleepSwitch()).intValue() == 1) {//开启
                // 获取当前时间
                LocalDateTime now_localDateTime = LocalDateTime.now();
                List<SleepRule> sleepRules = operatorPlanDeviceRule.getSleepRules();
                if (sleepRules != null && sleepRules.size() > 0) {
                    for (SleepRule sleepRule : sleepRules) {
                        String minTime = sleepRule.getMinTime();
                        String maxTime = sleepRule.getMaxTime();
                        //判断当前时间是否在自动任务开启时间区间内
                        boolean withinTimeInterval = Tools.isWithinTimeInterval(now_localDateTime, LocalTime.parse(minTime), LocalTime.parse(maxTime));
                        //在区间内，不处理任务业务
                        if (withinTimeInterval) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    /**
     * 获取两个数之间的随机数
     *
     * @param rateMin
     * @param rateMax
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static int randomRate(Integer rateMin, Integer rateMax) {
        //获取最小和最大时间的随机数
        int rateRandom = (int) (Math.random() * (rateMax - rateMin + 1) + rateMin);
        return rateRandom;
    }

    /**
     * 获取休眠的延迟时间
     *
     * @param operatorPlanDeviceRule
     * @return null
     * @author jitwxs
     * @date 2024/4/14 16:51
     */
    public static int getSleepTimeDelay(OperatorPlanDeviceRule operatorPlanDeviceRule, int delay, Integer startTime) {

        if (operatorPlanDeviceRule == null) {
            return delay;
        }

        if (startTime == null || startTime == 0) {
            startTime = DateUtil.getTime();
        }

        //新的开始时间
        startTime = startTime + delay;

        //计算新的开始是否在休眠时间内，如果在，则计算出新的开始时间与休眠时间的差值

        //判断新发送时间是否在允许区间内，不在则跳过
        Instant instant_sendTimeNew = Instant.ofEpochMilli(startTime * 1000L);
        LocalDateTime localDateTime_sendTimeNew = LocalDateTime.ofInstant(instant_sendTimeNew, ZoneId.systemDefault());
        //休眠开关（0关，1开）
        if (Tools.getInteger(operatorPlanDeviceRule.getSleepSwitch()) == 1) { //启动
            List<SleepRule> sleepRules = operatorPlanDeviceRule.getSleepRules();
            if (sleepRules != null && sleepRules.size() > 0) {
                for (SleepRule sleepRule : sleepRules) {
                    String minTime = sleepRule.getMinTime();
                    String maxTime = sleepRule.getMaxTime();
                    //判断当前时间是否在自动任务开启时间区间内
                    LocalTime minTimeLocalTime = LocalTime.parse(minTime);
                    LocalTime maxTimeLocalTime = LocalTime.parse(maxTime);
                    boolean withinTimeInterval = Tools.isWithinTimeInterval(localDateTime_sendTimeNew, minTimeLocalTime, maxTimeLocalTime);
                    //在区间内，则表示休眠
                    if (withinTimeInterval) {
                        LocalTime nowLocalTime = localDateTime_sendTimeNew.toLocalTime();
                        long delay_new = Tools.betweenSeconds(nowLocalTime, minTimeLocalTime, maxTimeLocalTime);
                        //在休眠时间内，则需要累加delay_new
                        if (delay_new > 0) {
                            delay = delay + (int) delay_new;
                        }
                    }
                }
            }
        }
        return delay;
    }

    /**
     * 验证是否Sop任务机器人休眠时间段（true-休眠；false-不休眠）
     *
     * @param sopDeviceRule
     * @Return （true-休眠；false-不休眠）
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static boolean verifySopDeviceSleepTime(SopDeviceRule sopDeviceRule, Date date) {
        if (sopDeviceRule != null) {
            //验证星期几时间
            if (Tools.getInteger(sopDeviceRule.getSleepWeekSwitch()).intValue() == 1) {//开启,则需要验证
                List<Integer> sleepWeekList = sopDeviceRule.getSleepWeekList();
                if (sleepWeekList != null && sleepWeekList.size() >= 0) {
                    //获取当前星期几
                    LocalDate localDate;
                    if(date != null){
                        localDate = TimeTools.getLocalDateFromDate(date);
                    }else{
                        localDate = LocalDate.now();
                    }
                    int dayOfWeek = localDate.getDayOfWeek().getValue();
                    if (sleepWeekList.contains(dayOfWeek)) {
                        return true;
                    }
                }
            }

            if (Tools.getInteger(sopDeviceRule.getSleepSwitch()).intValue() == 1) {//开启，则需要验证
                // 获取当前时间
                LocalDateTime localDateTime;
                if(date != null){
                    localDateTime = TimeTools.getLocalDateTimeFromDate(date);
                }else{
                    localDateTime = LocalDateTime.now();
                }

                List<SleepRule> sleepRules = sopDeviceRule.getSleepRules();
                if (sleepRules != null && sleepRules.size() > 0) {
                    for (SleepRule sleepRule : sleepRules) {
                        String minTime = sleepRule.getMinTime();
                        String maxTime = sleepRule.getMaxTime();
                        //判断当前时间是否在自动任务开启时间区间内
                        boolean withinTimeInterval = Tools.isWithinTimeInterval(localDateTime, LocalTime.parse(minTime), LocalTime.parse(maxTime));
                        //在区间内，不处理任务业务
                        if (withinTimeInterval) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }

    /**
     * 验证是否是机器人休眠时间段（true-休眠；false-不休眠）
     *
     * @param newCustomerPlanRule
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static boolean verifyFriendReqSleepTime(NewCustomerPlanRule newCustomerPlanRule) {
        if (newCustomerPlanRule != null) {
            if (Tools.getInteger(newCustomerPlanRule.getSleepSwitch()).intValue() == 1) {//开启
                // 获取当前时间
                LocalDateTime now_localDateTime = LocalDateTime.now();
                List<SleepRule> sleepRules = newCustomerPlanRule.getSleepRules();
                if (sleepRules != null && sleepRules.size() > 0) {
                    for (SleepRule sleepRule : sleepRules) {
                        String minTime = sleepRule.getMinTime();
                        String maxTime = sleepRule.getMaxTime();
                        //判断当前时间是否在自动任务开启时间区间内
                        boolean withinTimeInterval = Tools.isWithinTimeInterval(now_localDateTime, LocalTime.parse(minTime), LocalTime.parse(maxTime));
                        //在区间内，不处理任务业务
                        if (withinTimeInterval) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }


}
